package com.mybase.core;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;

public class MyFile extends File {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5045692598619551130L;

	public MyFile(String pathname) {
		super(pathname);
		// TODO Auto-generated constructor stub
	}

	public static String read(String filepath) throws IOException {
		FileInputStream fstream = null;
		DataInputStream in = null;
		BufferedReader br = null;
		String resume = "";
		try {
			fstream = new FileInputStream(filepath);
			in = new DataInputStream(fstream);
			br = new BufferedReader(new InputStreamReader(in));
			String strLine;

			while ((strLine = br.readLine()) != null) {
				resume += strLine + "\n";
			}
		} catch (Exception e) {
			fstream.close();
			in.close();
			br.close();
		} finally {
			fstream.close();
			in.close();
			br.close();
		}
		return resume;
	}

	public boolean write(String filePath, String content) {
		try {
			File file = new File(filePath);
			// if file doesnt exists, then create it
			if (!file.exists()) {
				file.createNewFile();
			}
			FileWriter fw = new FileWriter(file.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(content);
			bw.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean append(String filePath, String content) {
		try {
			File file = new File(filePath);
			if (file.exists()) {
				String filecontent = read(filePath) + "\n" + content;
				content = filecontent;
			}
			// if file doesnt exists, then create it
			if (!file.exists()) {
				file.createNewFile();
			}
			FileWriter fw = new FileWriter(file.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(content);
			bw.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	// throws Read-Only cant delete exception
	public boolean delete(String filePath) {
		return false;
	}

	public boolean rename(String sourceFileName, String destinationFileName) {
		return false;
	}

	public List<String> listDirectory(String directoryPath) {
		return null;
	}

	public HashMap<String, String> recursivelyListDirectory(String directoryPath) {
		return null;
	}

	public boolean hasFile(String fileName) {
		return false;
	}

	public boolean hasDirectory(String fileName) {
		return false;
	}

	public boolean mkdir(String directoryName) {
		return false;
	}

	// present working directory
	public String pwd() {
		return null;
	}

	public HashMap<String, String> listAttributes() {
		return null;
	}
}
